# Maintainer: Felix Yan <felixonmars@archlinux.org>

pkgname=mighttpd2
pkgver=4.0.3
pkgrel=62
pkgdesc="High performance web server on WAI/warp"
url="https://kazu-yamamoto.github.io/mighttpd2/"
license=("BSD")
arch=('loong64' 'x86_64')
depends=('ghc-libs' 'haskell-async' 'haskell-auto-update' 'haskell-base16-bytestring'
         'haskell-byteorder' 'haskell-case-insensitive' 'haskell-conduit' 'haskell-conduit-extra'
         'dhall' 'haskell-http-client' 'haskell-http-date' 'haskell-http-types' 'haskell-network'
         'haskell-old-locale' 'haskell-quic' 'haskell-resourcet' 'haskell-split'
         'haskell-streaming-commons' 'haskell-time-manager' 'haskell-tls'
         'haskell-tls-session-manager' 'haskell-unix-time' 'haskell-unliftio'
         'haskell-unordered-containers' 'haskell-wai' 'haskell-wai-app-file-cgi'
         'haskell-wai-logger' 'haskell-warp' 'haskell-warp-quic' 'haskell-warp-tls')
makedepends=('ghc' 'uusi' 'haskell-hspec')
source=("https://hackage.haskell.org/packages/archive/$pkgname/$pkgver/$pkgname-$pkgver.tar.gz")
sha256sums=('1a43390e921ab1b1b473d5bf65b2dcf27d0a8466e3243c2dab5d0c3de32bf9e1')

prepare() {
  cd $pkgname-$pkgver
  # https://github.com/kazu-yamamoto/mighttpd2/pull/25
  sed -i -e 's|"127.0.0.1","::1"|"0.0.0.0","::"|' test/ConfigSpec.hs
}

build() {
  cd $pkgname-$pkgver

  runhaskell Setup configure -O --enable-shared --enable-executable-dynamic --disable-library-vanilla \
    --prefix=/usr --docdir=/usr/share/doc/$pkgname --datasubdir=$pkgname --enable-tests \
    --dynlibdir=/usr/lib --libsubdir=\$compiler/site-local/\$pkgid --ghc-option=-fllvm \
    --ghc-option=-optl-Wl\,-z\,relro\,-z\,now \
    --ghc-option='-pie' \
    -fdhall -fquic -ftls

  runhaskell Setup build $MAKEFLAGS
  runhaskell Setup register --gen-script
  runhaskell Setup unregister --gen-script
  sed -i -r -e "s|ghc-pkg.*update[^ ]* |&'--force' |" register.sh
  sed -i -r -e "s|ghc-pkg.*unregister[^ ]* |&'--force' |" unregister.sh
}

check() {
  cd $pkgname-$pkgver
  runhaskell Setup test --show-details=direct
}

package() {
  cd $pkgname-$pkgver

  install -D -m744 register.sh "$pkgdir"/usr/share/haskell/register/$pkgname.sh
  install -D -m744 unregister.sh "$pkgdir"/usr/share/haskell/unregister/$pkgname.sh
  runhaskell Setup copy --destdir="$pkgdir"
  install -D -m644 LICENSE -t "$pkgdir"/usr/share/licenses/$pkgname/
  rm -f "$pkgdir"/usr/share/doc/$pkgname/LICENSE
}
